package com.srit.vgt.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.srit.vgt.entity.City;
import com.srit.vgt.entity.District;
import com.srit.vgt.entity.EcrInfo;
import com.srit.vgt.entity.EcrWeigh;
import com.srit.vgt.entity.Market;
import com.srit.vgt.entity.MarketTran;
import com.srit.vgt.main.DaoInject;
import com.srit.vgt.service.IServerPointService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Slf4j
public class ServerPointService extends DaoInject implements IServerPointService {

    @Override
    public String uploadBase(JSONObject all) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        JSONArray ecrInfos = all.getJSONArray("EcrInfo");

        Long time = all.getLong("time");
        Market market = new Market();
        JSONObject marketJson = all.getJSONObject("market");
        String marketId = marketJson.getString("marketId");
        market.setMarketId(marketId);
        market.setBusiNum(marketJson.getInteger("busiNum"));
        market.setMarketName(marketJson.getString("marketName"));
        market.setAddress(marketJson.getString("address"));
        market.setLatitude(marketJson.getDouble("latitude"));
        market.setLongitude(marketJson.getDouble("longitude"));
        market.setCityId(marketJson.getString("cityId"));
        market.setDistrictId(marketJson.getString("districtId"));
        market.setUpdateTime(new Date(time));
        market.setModels(all.getString("models"));
        market.setEcrNum(ecrInfos.size());
        marketDao.save(market);

        MarketTran marketTran = new MarketTran();
        marketTran.setMarketId(marketId);
        marketTran.setTranNum(all.getInteger("tranNum"));
        marketTran.setDate(format.format(new Date(time - 24 * 60 * 60 * 1000)));
        marketTran.setTranPrice(all.getDouble("tranPrice"));
        marketTranDao.save(marketTran);

        City city = new City();
        JSONObject cityJson = all.getJSONObject("city");
        city.setCityId(cityJson.getString("cityId"));
        city.setCityName(cityJson.getString("cityName"));
        cityDao.save(city);

        District district = new District();
        JSONObject districtJson = all.getJSONObject("district");
        district.setDistrictId(districtJson.getString("districtId"));
        district.setCityId(districtJson.getString("cityId"));
        district.setDistrictName(districtJson.getString("districtName"));
        districtDao.save(district);



        for (int i = 0; i < ecrInfos.size(); i++) {
            JSONObject ecrInfoJson = ecrInfos.getJSONObject(i);
            EcrInfo ecrInfo = new EcrInfo();
            ecrInfo.setEcrID(marketId + ecrInfoJson.getString("ecrID"));
            ecrInfo.setEcrType(ecrInfoJson.getString("ecrType"));
            ecrInfo.setMarketId(marketId);
            ecrInfo.setClassifyType(ecrInfoJson.getInteger("classifyType"));
            ecrInfo.setInsertDateTime(new Date(ecrInfoJson.getLong("insertDateTime")));
            ecrInfo.setStatusStartTime(new Date(ecrInfoJson.getLong("statusStartTime")));
            ecrInfo.setStatus(ecrInfoJson.getInteger("status"));
            ecrInfoDao.save(ecrInfo);

            EcrWeigh ecrWeigh = new EcrWeigh();
            ecrWeigh.setWeighNum(ecrInfoJson.getInteger("weighNum"));
            ecrWeigh.setWeighPrice(ecrInfoJson.getDouble("weighPrice"));
            ecrWeigh.setDate(format.format(new Date(time - 24 * 60 * 60 * 1000)));
            ecrWeigh.setEcrId(marketId + ecrInfoJson.getString("ecrID"));
            ecrWeighDao.save(ecrWeigh);

        }
        return "122.51.239.199";
    }
}
